package cn.com.oil.common.util;

import cn.com.oil.fx.FX;
import cn.hutool.json.JSONObject;
import javafx.application.Platform;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.TreeMap;

@Component
public class FxManager {

    private static final TreeMap<String, FX> FX_MAP = new TreeMap<>();


    public static void init() {
        JSONObject entries = FileUtils.getCacheJSon();
        Boolean isHasProject = entries.get("isHasProject", Boolean.class);
        String name;
        String url;
        if (isHasProject) {
            name = entries.get("masterWindowsName", String.class);
            url = entries.get("masterUrl", String.class);
        } else {
            name = entries.get("initWindowsName", String.class);
            url = entries.get("initUrl", String.class);
            FileUtils.initCreateDir();
        }
        FX fx = new FX(name, url);
        addFx(name, fx);
        FX.open();
    }

    public static boolean hasFx(String name) {
        return FX_MAP.containsKey(name);
    }

    // 获取窗口数量
    public Map<String, FX> getFxMap() {
        return FX_MAP;
    }

    // 添加窗口计数方法
    public static void addFx(String name, FX fx) {
        FX_MAP.put(name, fx);
    }

    // 自定义添加窗口
    public static void addFx(String name) {
        if (!FileUtils.isExistDir(name)) {
            FileUtils.CreateDir(name);
        }
        String initUrl = FileUtils.getCacheJSon().get("initUrl", String.class);
        String nameUrl = String.format("%s/%s", initUrl, name);
        FX fx = new FX();
        fx.openNewWindow(name, nameUrl);
        addFx(name, fx);
    }

    public static void changeFx(String originName, String name) {
        if (!FileUtils.isExistDir(name)) {
            FileUtils.CreateDir(name);
        }
        JSONObject cacheJSon = FileUtils.getCacheJSon();
        String initUrl = cacheJSon.get("initUrl", String.class);
        String nameUrl = String.format("%s/%s", initUrl, name);
        FX fx = getFx(originName);
        Platform.runLater(() -> {
            fx.getStage().setScene(fx.commonScene(nameUrl));
        });
        fx.setUrl(nameUrl);
        FX_MAP.remove(originName);
        addFx(name, fx);
        JSONObject entries = FileUtils.getCacheJSon();
        entries.set("masterWindowsName", FX_MAP.lastKey());
        entries.set("masterUrl", ((FX) FX_MAP.lastEntry().getValue()).getUrl());
        entries.set("isHasProject", Boolean.TRUE);
        FileUtils.setCacheJSon(entries);
    }


    // 删除方法
    public static void deleteFx(String name) {
        FX_MAP.remove(name);
        JSONObject entries = FileUtils.getCacheJSon();
        entries.set("masterWindowsName", FX_MAP.lastKey());
        entries.set("masterUrl", ((FX) FX_MAP.lastEntry().getValue()).getUrl());
        entries.set("isHasProject", Boolean.TRUE);
        FileUtils.setCacheJSon(entries);
    }

    // 获取窗口数量
    public static int getFxCount() {
        return FX_MAP.size();
    }

    public static FX getFx(String name) {
        return FX_MAP.get(name);
    }
}
